package com.prj.ufdm.demo.tutorial.mongo;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.bson.Document;
import org.springframework.data.mongodb.gridfs.GridFsOperations;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;

public class MongoDBTest {
	
//	public static String HOST = "192.168.72.129";
	public static String HOST = "192.168.128.128";
    public static Integer PORT = 27017;

    public static void main(String[] args) {
    	
    	MongoClient mongoClient = getConnect(HOST,PORT);
    	
        try {
        	
            //连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("my_first_db");

            // 获取 Collection
            MongoCollection<Document> collection = mongoDatabase.getCollection("file_table");
            
            // 新增文档
            collection.insertMany(createDacuments());
            System.out.println("文档插入成功");

            // 查询文档
            FindIterable<Document> findIterable = collection.find();
            MongoCursor<Document> mongoCursor = findIterable.iterator();
            while (mongoCursor.hasNext()) {
                System.out.println(mongoCursor.next());
            }
            System.out.println("检索所有文档成功");

            //更新文档
            collection.updateMany(Filters.eq("fileType", "xls"), new Document("$set", new Document("fileType", "xlsx")));
            System.out.println("更新文档成功");

            //删除文档
            collection.deleteMany(Filters.eq("fileType", "xlsx"));
            System.out.println("删除文档成功");

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
        finally {
        	if(mongoClient!=null) mongoClient.close();
        }
    }
    
    public static List<Document> createDacuments(){
    	
    	List<Document> documentList = new ArrayList<Document>();
        Document document1 = new Document("id", UUID.randomUUID().toString()).
                append("fileName", "世界地图").
                append("filePath", "/opt/dadfa/asdf").
                append("fileHash", "DFDSASFDFDKAFJSIFASDLFASDS").
                append("fileType", "JPG").
                append("fileSize", 100).
                append("by", "Fly");
        
        Document document2 = new Document("id", UUID.randomUUID().toString()).
                append("fileName", "年度报表").
                append("filePath", "/opt/dadfa/asdf").
                append("fileHash", "DFDSASFDFDKAFJSIFASDLFASDS").
                append("fileType", "xls").
                append("fileSize", 100).
                append("by", "Fly");
        documentList.add(document1);
        documentList.add(document2);
        return documentList;
    }
    
    public static MongoClient getConnect(String host,Integer port,String database,String userName,String password) {
    	MongoClient mongoClient = null;
        ServerAddress serverAddress = new ServerAddress(HOST, PORT);
        final List<ServerAddress> addressList = new ArrayList<ServerAddress>();
        addressList.add(serverAddress);
        if(userName!=null && !userName.equals("") && password!=null && !password.equals("")) {
            MongoCredential credential = MongoCredential.createScramSha1Credential(database, userName, password.toCharArray());
            List<MongoCredential> credentials = new ArrayList<MongoCredential>();
            credentials.add(credential);
            MongoClientOptions clientOptions = new MongoClientOptions.Builder().build();
            mongoClient = new MongoClient(addressList, credentials,clientOptions);
        }
        mongoClient = new MongoClient(addressList);
        return mongoClient;
    }
    
    public static MongoClient getConnect(String host,Integer port) {
        return getConnect(host,port,null,null,null);
    }
}
